<template>
  <div class="update-name">
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      class="page-nav-bar"
      @click-left="$emit('close')"
      @click-right="onUpdate"
    />
    <div class="field-wrap">
      <van-field
        v-model="localName"
        row="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js'
export default {
  name: 'WorkspaceJsonUpdateName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },

  mounted () {},

  methods: {
    async onUpdate () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const localName = this.localName
        if (!localName.length) {
          this.$toast('昵称不能为空')
          return
        }
        await updateUserProfile({
          name: localName
        })
        this.$emit('input', localName)
        this.$emit('close')
        this.$toast('更新成功')
      } catch (err) {
        console.log(err)
        this.$toast('更新失败，请重试')
      }
      this.$emit('close')
    }
  }
}
</script>

<style scoped lang="less">
/deep/.van-nav-bar__text {
  color: #fff;
}
/deep/.van-cell__value {
  background-color: #f3f3f3;
  font-size: 36px;
}
</style>
